package com.idroc.coelacanth.core.dht;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 一致性hash环节点动作 默认观察者
 * @author yuyoo (yuyoo4j@163.com)
 * @date 2009-12-16 下午01:16:57
 */
public class DefaultNodeActionObserver implements NodeActionObserver {

	private static final Logger log = LoggerFactory.getLogger(DefaultNodeActionObserver.class);
	
	public void update(NodeAction na, int nodeNo,
			ConsistentHashRing ring) { 
		
		log.debug("一致性hash节点[{}]发起动作[{}]", nodeNo, na); 
		if (ring.getCurrentNode().getNo() == nodeNo) {
			switch (na) {
			case INSERT:
			case OPEN:
			case REOPEN:
				log.debug("当前节点[{}]开放", nodeNo);
				ring.getOpens().put(Integer.valueOf(nodeNo), ring.getCurrentNode());
				break;
			}
		} else {
			switch (na) {
			case INSERT:
			case OPEN:
			case REOPEN:
				log.debug("当前节点[{}]开放", nodeNo);
				ring.getOpens().put(Integer.valueOf(nodeNo), ring.getCurrentNode());
				break;
			}
		}
		log.debug("SEEDS[{}]", ring.getSeeds());
		log.debug("OPENS[{}]", ring.getOpens());
		log.debug("CLOSES[{}]", ring.getClass());
	}		
}
